In [95]:
import re
import subprocess
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.graph_objs as go
import gpxpy
import random
from dateutil.parser import parse
from copy import deepcopy
from imageio import imread
from matplotlib.pyplot import imshow
from plotly.offline import init_notebook_mode, iplot

init_notebook_mode(connected=True)
%matplotlib inline
In [79]:
img = imread('Downloads/basemap.png') / 255
plt.figure(figsize = (10,10))
imshow(img)
print(f"Size: {img.shape[0]}x{img.shape[1]}x{img.shape[2]} {img.dtype}")
Size: 783x1010x4 float64
In [17]:
!pip install gpxpy
Collecting gpxpy
  Downloading gpxpy-1.4.1.tar.gz (105 kB)
     |████████████████████████████████| 105 kB 3.3 MB/s eta 0:00:01
Building wheels for collected packages: gpxpy
  Building wheel for gpxpy (setup.py) ... done
  Created wheel for gpxpy: filename=gpxpy-1.4.1-py3-none-any.whl size=42770 sha256=47f4638b59e15a6d5c7b0a9761e7cce78332e6dd0df02a45cdf7c96ab18d1eac
  Stored in directory: /Users/adam.etches/Library/Caches/pip/wheels/68/72/e9/80ea8b34d4a53a209afd72d96137d12a88ea7ee63d0c3755ed
Successfully built gpxpy
Installing collected packages: gpxpy
Successfully installed gpxpy-1.4.1
In [11]:
def show_colors(colors):
    # colors_matrix = np.reshape(colors, [4, n_colors // 4, 3])
    imshow(np.reshape(colors, (1, -1, 3)), aspect='auto')
    plt.xticks([])
    plt.yticks([])
    plt.gcf().set_size_inches(10, 1)
show_colors(colors)
In [12]:
# Find and show unique colors
img_array = img[:, :, :3].reshape((img.shape[0] * img.shape[1], 3))
colors = np.unique(img_array, axis=0)
n_colors = colors.shape[0]

def show_colors(colors):
    # colors_matrix = np.reshape(colors, [4, n_colors // 4, 3])
    imshow(np.reshape(colors, (1, -1, 3)), aspect='auto')
    plt.xticks([])
    plt.yticks([])
    plt.gcf().set_size_inches(10, 1)
show_colors(colors)
In [13]:
# Create a custom colormap
color_to_value = {tuple(color[:3]): i / (n_colors - 1) for i, color in enumerate(colors)}
my_cmap_ply = [(value, 'rgb({}, {}, {})'.format(*color)) for color, value in color_to_value.items()]
In [14]:
# Map pixels to values
fun_find_value = lambda x: color_to_value[tuple(x[:3])]
values = np.apply_along_axis(fun_find_value, 2, np.flipud(img))
In [15]:
# Display terrain
yy = np.linspace(0, 1, img.shape[0])
xx = np.linspace(0, 1, img.shape[1])
zz = np.zeros(img.shape[:2])

surf = go.Surface(
    x=xx, y=yy, z=zz,
    colorscale=my_cmap_ply,
    surfacecolor=values,
    showscale=False
)
fig = go.Figure(data=[surf], layout=go.Layout())
iplot(fig, filename='terrain.html')
In [98]:
file = 'Downloads/Lunch_Run.gpx'
gpx_file = open(file, 'r')
gpx = gpxpy.parse(gpx_file)

df = pd.DataFrame(columns=['lon', 'lat', 'alt', 'time'])

d1 = parse('2020-05-29T11:12:49Z')

for segment in gpx.tracks[0].segments: # all segments

    data = segment.points

    for point in data:
        timesec = (abs((point.time - d1).seconds))
        df = df.append({'lon': point.longitude, 'lat' : point.latitude, 'alt' : point.elevation, 'time' : timesec}, ignore_index=True)
        #df = df.append({'lon': point.longitude, 'lat' : point.latitude, 'alt' : point.elevation, 'time' : point.time}, ignore_index=True)
        #df = df.append({'lon': random.randint(-7000,7000), 'lat' : random.randint(-7000,7000), 'alt' : point.elevation, 'time' : random.randint(-90,90)}, ignore_index=True)
        #df = df.append({'lon': point.longitude, 'lat' : point.latitude, 'alt' : point.elevation, 'time' : random.randint(-90,-85)}, ignore_index=True)
  
traces = []

#print((df['time'].first-df['time'].last.shift()).fillna(0))
#print(df['time'].first())
df.index = df['time']


trace = go.Scatter3d(
    
    x=df['lon'].values, y=df['lat'].values, z=df['time'].values,
    
    showlegend=False
)
traces.append(trace)   
In [100]:
yy = np.linspace(52.223, 52.242, img.shape[0])
#yy = np.linspace(-8000, +8000, img.shape[0])
xx = np.linspace(0.242, 0.284, img.shape[0])
#xx = np.linspace(-8000, +8000, img.shape[1])
zz = np.full(img.shape[:2], -90)


surf = go.Surface(
    x=xx, y=yy, z=zz,
    colorscale=my_cmap_ply,
    surfacecolor=values,
    showscale=False
)

layout = go.Layout(
    margin=dict(l=0,r=0,b=0,t=0),
    scene=go.layout.Scene(
        xaxis=go.layout.scene.XAxis(title='', showticklabels=False),
        yaxis=go.layout.scene.YAxis(title='', showticklabels=False),
        zaxis=go.layout.scene.ZAxis(title='Time (s)'),
        aspectratio=dict(x=1, y=1, z=1.3),
        camera=go.layout.scene.Camera(
            projection=go.layout.scene.camera.Projection(
                type='orthographic'
            )
        )
    )
)

fig = go.Figure(data=[surf] + traces, layout=layout)
iplot(fig, filename=f'terrain-64colors+paths.html')
In [ ]: